import {loadStdlib} from '@reach-sh/stdlib';
import * as backend from './build/index.main.mjs';
import { ask, yesno, done } from '@reach-sh/stdlib/ask.mjs';
import launchToken from '@reach-sh/stdlib/launchToken.mjs';
import { Buyer } from './build/index.main.mjs';
const stdlib = loadStdlib(process.env);
const timeoutK = stdlib.connector === 'ALGO' ? 1 : 3;


// const star_token = new Token({name:'star_token', symbol:'SRT' ,supply:10000000000000000});


(async () => {
  
  const startingBalance = stdlib.parseCurrency(100);

  //铸造新币
  // const star_token = await launchToken("star_token", "SRT");
  
  //确定身份
  const isBuyer = await ask(
    'Are you buyer?',
    yesno
  )

  const who = isBuyer ? 'Buyer' : 'Oracle';
  
  //登录
  let acc = null;
  const createAcc = await ask(
    `Would you like to create an account? (only possible on devnet)`,
    yesno
  );
  if (createAcc) {
    acc = await stdlib.newTestAccount(stdlib.parseCurrency(1000));
  } else {
    const secret = await ask(
      `What is your account secret?`,
      (x => x)
    );
    acc = await stdlib.newAccountFromSecret(secret);
  }

  
  const ctc = acc.deploy(backend);
  const info = await ctc.getInfo();

  //返回余额
  const fmt = (x) => stdlib.formatCurrency(x, 4);
  const getBalance = async () => fmt(await stdlib.balanceOf(acc));
  const before = await getBalance();

  const interact = { ...stdlib.hasRandom };

  interact.informTimeout = () => {
    console.log(`There was a timeout.`);
    process.exit(1);
  }

  interact.getInfo = () =>{
    return info;
  }
  
  
  
  if(who=='Buyer'){
    //购买NFT
    const choice = await ask(
      'Would you like to buy an Constellation NFT for 10 cfx?',
      yesno
    );


    if(choice==true){
      //确定购买
  
      interact.getAcct =  async () => {
        return acc;
      }
  
      interact.getToken = async () => {
        return star_token;
      }
      
      interact.buyNFT = async () => {
        const _time = Date.now();
        console.log(`Your balance is ${before}`);
        return _time; 
      }
  
      interact.generat_id = async () =>{
        const id = stdlib.randomUInt();
        console.log(`Buyer makes id #${id}`);
        const after = await getBalance();
        console.log(`Your balance is ${after}`);
        return id; 
      }
  
      
      interact.getTime = async () => {
        console.log('This is the time that the Buyer wants to use to generate the NFT')
        return _time;
      }
  
      interact.getlove = async () => {
        const love = await ask('Please enter the score of love according to the time',
        (x) => {return x})
      }
  
      interact.getcarrer = async () => {
        const carrer = await ask('Please enter the score of carrer according to the time',
        (x) => {return x})
      }
  
      interact.getlove = async () => {
        const fortune = await ask('Please enter the score of fortune according to the time',
        (x) => {return x})
      }
  
      interact.getId = async (id) => {
        return id;
      }
    } //

      interact.getRefund = async () =>{
        transfer(10).to(acc)
        console.log('your cfx has been refunded')
      }


  }else{
    //打分
      console.log('Please rate those NFTs for buyers');
  }

  

  if(who=='Buyer'){
    const showOrNot = await ask(
      'Would you like to see your NFT?',
      yesno
    );

    if(showOrNot==true){
      interact.showOwner = async (id,acc) =>{
        console.log(`${acc} is the owner of NFT:${id}`)
      }
    }

    const pledgeOrNot = await ask(
      'Would you like to pledge your NFT to get star token?',
      yesno
    );

    if(pledgeOrNot==true){
      interact.showNFT = async () => {
        console.log(`NFT:${id} has been shown to all`)
      }
      
      interact.pledgeNFT = async () => {
        const pledgeTime = await ask(
          'Please enter the time you wants to pledge',
          (x) => {return x}
        );
      console.log(`your NFT has been pledged for ${pledgeTime}`)
      //
      }

      interact.waitPledge = async () => {
        for (let i = 0; i < pledgeTime; i++ ) {
          console.log('waiting...');
          await stdlib.wait(1);
        } 
      }

      interact.informPledged = async (id,pledgeTime) =>{
        transfer(pledgeTime, star_token).to(this)
        console.log(`your NFT:${id} has been returned, your profit is ${pledgeTime}`)
      }
    }
  }

  const part = isBuyer ? backend.Buyer : backend.Oracle;
  await part(ctc, interact);

  done();
})();